import Vue from 'vue'
import App from './App'

//系统错误捕获
// const errorHandler = (errorTxt) => {
// 	//异常提示
// 	setTimeout(function() { 
// 		uni.showToast({
// 			title: errorTxt,
// 			mask: true,      
// 			icon: 'none',
// 			position: 'top',
// 			duration: 2000
// 		});
// 	}, 1500)
// }
// Vue.config.errorHandler = errorHandler;
// Vue.prototype.$throw = (errorTxt) => errorHandler(errorTxt, this);

//微信分享
import share from '@/static/js/share.js'
Vue.mixin(share)

// import uView from '@/uni_modules/uview-ui'
// Vue.use(uView)

//网络请求
Vue.prototype.baseUrl = 'https://guess.upwellcanada.com/';
// Vue.prototype.baseUrl = 'http://wang-dai-gang.51vip.biz/';//调试地址
import baseRequest from '@/common/BaseRequest.js'
Vue.prototype.baseRequest = baseRequest;
import tokenRequest from '@/common/TokenRequst.js'
Vue.prototype.tokenRequest = tokenRequest;


//执行登录
Vue.prototype.doLogin = function(upCode) {
	let that = this;
	uni.showLoading({
		title: '登录中'
	});
	uni.login({
		provider: 'weixin',
		success: function(loginRes) {
			uni.request({
				url: that.baseUrl + 'WXLogin/getLoginInfoByCode',
				method: 'POST',
				data: {
					code: loginRes.code,
					upCode: upCode
				},
				success: function(res) {
					let sessionKey = res.data.data;
					uni.setStorageSync('sessionKey', sessionKey); //缓存会话对象
				},
				complete: function() {
					setTimeout(function() {
						uni.hideLoading();
					}, 2000)
				}
			});
		},
		complete: function() {
			setTimeout(function() {
				uni.hideLoading();
			}, 2000)
		}
	});

}


//检验登录状态
Vue.prototype.VerifyLogin = function(upCode) {
	const sessionKey = uni.getStorageSync('sessionKey');
	let that = this;
	if ( upCode != undefined && null != upCode && upCode.length > 0) { //有推荐人到情况需要更新邀请人
		that.doLogin(upCode);
	} else {
		if (null == sessionKey || sessionKey.length == 0) {
			that.doLogin(upCode);
		} else {
			wx.checkSession({
				success() {
						uni.request({
							url: that.baseUrl + 'WXLogin/verifySessionKey',
							method: 'post',
							data: {
								sessionKey: sessionKey
							},
							success: function(res) {
								if(!res.data.data){
									that.doLogin(upCode);
								}
							}
						})
				},fail() {
					that.doLogin(upCode);
				}
			})
		}
	}
}

//验证手机号格式
Vue.prototype.VerifyTel = function(tel) {

	if (tel.length == 0) {
		uni.showToast({
			icon: 'none',
			title: '手机号不能为空',
			duration: 2000
		});
		return;
	}

	if (!(/^1[3456789]\d{9}$/.test(tel))) {
		uni.showToast({
			icon: 'none',
			title: '手机格式异常',
			duration: 2000
		});
		return false;
	}
	return true;
}

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
